package com.esgpro.esg_api.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.esgpro.esg_api.mapper.UserMapper;
import com.esgpro.esg_api.pojo.User;
import com.esgpro.esg_api.service.Info.UserServiceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserServiceInfo {
    @Autowired
    private UserMapper mapper;

    @Override
    public boolean insertOne(User user) {
        return mapper.insert(user) == 1;
    }

    @Override
    public boolean deleteOneById(int id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public User selectOneById(int id) {
        return mapper.selectById(id);
    }

    @Override
    public HashMap<String, Object> selectByMap(int current, int size, HashMap<String, Object> map) {
        IPage<User> page = new Page<>(current, size);
        page = mapper.selectPage(page, new QueryWrapper<User>().allEq(map));
        HashMap<String, Object> result = new HashMap<>();
        result.put("total", page.getTotal());
        result.put("list", page.getRecords());
        result.put("current", page.getCurrent());
        result.put("size", page.getSize());
        result.put("page", page.getPages());
        return result;
    }

    @Override
    public boolean updateById(User user) {
        return mapper.updateById(user) == 1;
    }

    @Override
    public List<User> selectAll() {
        return mapper.selectList(null);
    }

    @Override
    public User Login(int account, String password) {
        User user = mapper.selectOne(new QueryWrapper<User>().eq("account", account));
        if (user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }

    @Override
    public String getAccountByType(String type) {
        if (type.equals("个人账号"))
            return "01";
        else
            return "10";
    }

    @Override
    public int getUserAmount() {
        return mapper.selectList(null).size();
    }
}
